@echo off
chcp 65001 >nul
echo ========================================
echo Build with Hooks Solution
echo ========================================
echo.

REM Check Python
python --version >nul 2>&1
if errorlevel 1 (
    echo Error: Python not found
    pause
    exit /b 1
)

echo Python found
echo.

REM Install dependencies
echo Installing dependencies...
pip install pyinstaller opencv-python ultralytics torch torchvision

if errorlevel 1 (
    echo Dependencies installation failed
    pause
    exit /b 1
)

echo Dependencies installed
echo.

REM Clean previous builds
echo Cleaning previous builds...
if exist build rmdir /s /q build
if exist dist rmdir /s /q dist
if exist *.spec del *.spec

echo Cleaned
echo.

REM Create hook-cv2.py
echo Creating hook-cv2.py...
echo # -*- coding: utf-8 -*- > hook-cv2.py
echo from PyInstaller.utils.hooks import collect_data_files, collect_submodules >> hook-cv2.py
echo datas = collect_data_files('cv2') >> hook-cv2.py
echo hiddenimports = collect_submodules('cv2') >> hook-cv2.py

REM Create hook-ultralytics.py
echo Creating hook-ultralytics.py...
echo # -*- coding: utf-8 -*- > hook-ultralytics.py
echo from PyInstaller.utils.hooks import collect_data_files, collect_submodules >> hook-ultralytics.py
echo datas = collect_data_files('ultralytics') >> hook-ultralytics.py
echo hiddenimports = collect_submodules('ultralytics') >> hook-ultralytics.py

echo Hooks created
echo.

REM Build with hooks
echo Building with hooks...
pyinstaller --onefile --windowed --name VideoProcessingApp ^
    --additional-hooks-dir=. ^
    --hidden-import cv2 ^
    --hidden-import cv2.cv2 ^
    --hidden-import cv2.cv2.cv2 ^
    --hidden-import cv2.cv2.cv2.cv2 ^
    --hidden-import ultralytics ^
    --hidden-import ultralytics.yolo ^
    --hidden-import ultralytics.yolo.engine ^
    --hidden-import ultralytics.yolo.engine.model ^
    --hidden-import ultralytics.yolo.utils ^
    --hidden-import ultralytics.yolo.utils.ops ^
    --hidden-import ultralytics.yolo.utils.torch_utils ^
    --hidden-import ultralytics.yolo.utils.checks ^
    --hidden-import ultralytics.yolo.utils.files ^
    --hidden-import ultralytics.yolo.utils.plotting ^
    --hidden-import ultralytics.yolo.utils.callbacks ^
    --hidden-import ultralytics.yolo.utils.loss ^
    --hidden-import ultralytics.yolo.utils.metrics ^
    --hidden-import ultralytics.yolo.utils.segment ^
    --hidden-import ultralytics.yolo.utils.tal ^
    --hidden-import ultralytics.yolo.utils.transforms ^
    --hidden-import torch ^
    --hidden-import torchvision ^
    --hidden-import torchvision.transforms ^
    --hidden-import torchvision.transforms.functional ^
    --hidden-import PIL ^
    --hidden-import PIL._tkinter_finder ^
    --hidden-import PIL.Image ^
    --hidden-import PIL.ImageDraw ^
    --hidden-import PIL.ImageFont ^
    --hidden-import tkinter ^
    --hidden-import tkinter.filedialog ^
    --hidden-import tkinter.messagebox ^
    --hidden-import tkinter.ttk ^
    --hidden-import pathlib ^
    --hidden-import threading ^
    --hidden-import subprocess ^
    --hidden-import shutil ^
    --hidden-import time ^
    --hidden-import random ^
    --hidden-import sys ^
    --hidden-import os ^
    --hidden-import mystep1v2 ^
    --hidden-import numpy ^
    --hidden-import yaml ^
    --hidden-import requests ^
    --hidden-import tqdm ^
    --hidden-import matplotlib ^
    --hidden-import matplotlib.pyplot ^
    --hidden-import matplotlib.backends.backend_tkagg ^
    --collect-all cv2 ^
    --collect-all ultralytics ^
    --collect-all torch ^
    --collect-all torchvision ^
    --add-data "mystep1v2.py;." ^
    --exclude-module matplotlib.tests ^
    --exclude-module numpy.tests ^
    --exclude-module PIL.tests ^
    --exclude-module tkinter.test ^
    --exclude-module unittest ^
    --exclude-module test ^
    --exclude-module tests ^
    gui_app_english.py

if errorlevel 1 (
    echo Build failed
    pause
    exit /b 1
)

echo.
echo ========================================
echo Build successful!
echo ========================================
echo exe file: dist\VideoProcessingApp.exe
echo.
echo Usage instructions:
echo 1. Make sure FFmpeg is installed on target machine
echo 2. Make sure model file pad230.pt is in correct location (D:/pad230.pt)
echo 3. Network connection needed for first run
echo 4. First run may take time to load model
echo.
pause 